<template>
	<u-modal v-model="show" :show-cancel-button="true" confirm-text="升级" title="发现新版本" @cancel="cancel" @confirm="confirm">
		<view class="u-update-content"><rich-text :nodes="content"></rich-text></view>
		<u-toast ref="uToast"></u-toast>
	</u-modal>
</template>

<script>
export default {
	data() {
		return {
			show: false,
			item: '',
			content: ``
		};
	},
	onLoad(option) {
		if (option) {
			this.item = JSON.parse(decodeURIComponent(option.item));
			console.log(this.item);
			this.content = this.item.content;
		}
	},
	onReady() {
		this.show = true;
	},
	methods: {
		cancel() {
			this.closeModal();
		},
		confirm() {
			// #ifdef APP-PLUS
			var dtask = plus.downloader.createDownload(this.item.url, {}, function(d, status) {
				// 下载完成
				if (status == 200) {
					plus.runtime.install(
						plus.io.convertLocalFileSystemURL(d.filename),
						{ force: true },
						function(res) {
							uni.showToast({
								title: '更新成功!',
								duration: 1500
							});
							plus.runtime.restart();
							// plus.runtime.quit();
						},
						function(error) {
							uni.showToast({
								title: '安装失败',
								duration: 1500
							});
						}
					);
				} else {
					uni.showToast({
						title: '更新失败',
						duration: 1500
					});
				}
			});
			dtask.start();
			// #endif
			// uni.downloadFile({
			// 	//执行下载
			// 	url: this.item.url,
			// 	success: downloadResult => {
			// 		//下载成功
			// 		console.log('00')
			// 	}
			// });
		},
		closeModal() {
			uni.navigateBack();
		}
	}
};
</script>

<style scoped lang="scss">
@import '../../libs/css/style.components.scss';

.u-full-content {
	background-color: #00c777;
}

.u-update-content {
	font-size: 26rpx;
	color: $u-content-color;
	line-height: 1.7;
	padding: 30rpx;
}
</style>
